<template>
  <div class="operation">
    <van-search placeholder="写评论···" left-icon="edit" shape="round" v-model="value" :clearable="true" @search="writeComment" />
    <div class="oper-box">
      <div class="to-comment" @click="toComment"><van-icon name="chat-o" /></div>
      <div @click="clickCollect">
        <van-icon name="star" v-if="isCollect" />
        <van-icon name="star-o" v-else />
      </div>
      <div @click="clickLike">
        <van-icon name="good-job" v-if="isLike" />
        <van-icon name="good-job-o" v-else />
      </div>
      <van-icon name="share" />
    </div>
  </div>
</template>
<script>
export default {
  data: () => ({
    value: '',
    isCollect: false,
    isLike: false
  }),
  props: { id: { type: String } },
  created () { },
  methods: {
    clickCollect () {
      this.isCollect = !this.isCollect
      if (this.isCollect) {
        this.$toast('收藏成功')
      } else {
        this.$toast('取消收藏')
      }
      return this.isCollect
    },
    clickLike () {
      this.isLike = !this.isLike
      if (this.isLike) {
        this.$toast('点赞成功')
      } else {
        this.$toast('取消点赞')
      }
      return this.isLike
    },
    toComment () {
      let eleTop = document.querySelector('.comment')
      document.documentElement.scrollTop = eleTop.offsetTop
    },
    async writeComment (value) {
      let { u_id: num } = JSON.parse(localStorage.getItem('users'))[0]
      console.log(num)
      const { data: { status } } = await this.$http.get(`/api/addcomment?u_id=${num}&new_id=${this.id}&title=${value}`)
      if (status !== 0) {
        return this.$toast('评论失败')
      }
      // 刷新页面评论
      this.$emit('refreshCom', true)
      this.$toast('评论成功')
    }
  }
}
</script>
<style lang="less" scoped>
.operation {
  position: fixed;
  bottom: 0;
  display: flex;
  justify-content: space-between;
  width: 100%;
  height: 50px;
  align-items: center;
  background-color: #fff;
  .van-search{
    width: 220px;
  }
  .oper-box{
    display: flex;
    justify-content: space-evenly;
    width: 150px;
    .van-icon {
      font-size: 18px;
    }
  }
}
</style>
